package cn.uniquesoft.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;



public class DateUtil {

	/** DateFormat缓存 */
	private static Map<String, DateFormat> dateFormatMap = new HashMap<String, DateFormat>();
	public static String date_format_full = "yyyyMMddHHmmssSSS";
	/** 日期格式(yyyyMMddHHmmss) */
	public static final String yyyyMMddHHmmss_EN = "yyyyMMddHHmmss";
	public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
	private static Logger logger = Logger.getLogger(DateUtil.class);
	public static String date2String(Date date, String format) {
		String result = null;
		if (date == null) {
			return result;
		}
		if (StringUtils.isEmpty(format))
			format = "yyyy-MM-dd HH:mm:ss";
		try {
			result = DateFormatUtils.format(date, format);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	public static void main(String[] args) {
		System.out.println(date2String(new Date(), yyyyMMddHHmmss_EN));
	}
	
	public static String date2timestamp(Date date,String format){
		Date date1 = new Date();
		String result = null;
		if (date == null) {
			return result;
			}
		if (StringUtils.isEmpty(format)){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		try{
			result = DateFormatUtils.format(date1, format);
			result = result.replace("-", "");
			result = result.replace(":", "");
			result = result.replace(" ", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
			return result;
		
	}
	
	
	public static Date string2Date(String str, String format) {
		if (StringUtils.isEmpty(str)) {
			return null;
		}
		Date result = null;
		String[] formats = (String[]) null;
		if (StringUtils.isEmpty(format)) {
			formats = new String[3];
			formats[0] = "yyyy-MM-dd HH:mm:ss";
			formats[1] = "yyyy-MM-dd";
			formats[2] = "HH:mm:ss";
		} else {
			formats = new String[4];
			formats[0] = format;
			formats[1] = "yyyy-MM-dd HH:mm:ss";
			formats[2] = "yyyy-MM-dd";
			formats[3] = "HH:mm:ss";
		}
		try {
			result = DateUtils.parseDate(str, formats);
		} catch (Exception ex) {
			logger.warn("日期或时间格式不正确，日期时间字符串：" + str + "， 格式：" + format);
		}

		return result;
	}
	//获取当前时间戳
	public static long getCurrentMills(){
		Calendar calendar = Calendar.getInstance();
		return calendar.getTimeInMillis();
	}
	/**
	 * 指定日期加上天数后的日期
	 * @param num 为增加的天数
	 * @param currdate 创建时间
	 * @return
	 * @throws ParseException
	 */
	public static Date plusDay(int num, Date currdate) throws ParseException {
		Calendar ca = Calendar.getInstance();
		ca.setTime(currdate);
		ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
		currdate = ca.getTime();
		return currdate;
	}

	/**
	 * 今天与某个日期之间的天数
	 */
	public static int daysBetween(String date) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date newDate = format.parse(date);
		Date curDate = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(newDate);
		long newTime = calendar.getTimeInMillis();
		calendar.setTime(curDate);
		long curTime = calendar.getTimeInMillis();
		long between = (newTime - curTime) / (1000 * 3600 * 24);//天数
		return Integer.parseInt(String.valueOf(between));
	}

	/**
	 * 将Date转换成字符串“yyyymmddhhmmss”的字符串
	 * @param date
	 * @return
	 */
	public static String dateToDateFullString(Date date) {
		if (null == date)
			return null;
		else
			return dateToDateString(date, yyyyMMddHHmmss_EN);
	}

	/**
	 * 将Date转换成formatStr格式的字符串
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String dateToDateString(Date date, String formatStr) {
		DateFormat df = getDateFormat(formatStr);
		return df.format(date);
	}


	/**
	 * 获取DateFormat
	 * @param dateTimeStr
	 * @param formatStr
	 * @return
	 */
	public static DateFormat getDateFormat(String formatStr) {
		DateFormat df = dateFormatMap.get(formatStr);
		if (df == null) {
			df = new SimpleDateFormat(formatStr);
			dateFormatMap.put(formatStr, df);
		}
		return df;
	}
}
